﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Net;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security.Permissions;

namespace TridentReport.Net
{
    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    class watcher
    {
        #region private
        private FileSystemWatcher seer;
        private string _searchPath;
        private bool _isrunning;
        private string _writePath;
        #endregion

        #region Public properties
        /// <summary>
        /// Gets of sets the Path the search for log file changes
        /// </summary>
        public string SearchPath
        {
            get { return _searchPath; }
        }

        /// <summary>
        /// Return true is the watcher is active  or false if it isn't.
        /// </summary>
        public bool IsRunning
        {
            get { return _isrunning; }
        }

        /// <summary>
        /// Gets or sets the path to write the log file to when the watched folder changes
        /// </summary>
        public string WritePath
        {
            get { return _writePath; }
            set { _writePath = value; }
        }
        #endregion


        //console application
        /// <summary>
        /// Starts a new folder watcher.  Watches the folder it is launched from unless you specify a path on the command line
        /// </summary>
        public watcher(string SearchPath)
        {
            _searchPath = SearchPath;
            if (_searchPath.Length == 0) _searchPath = ".";

            // setup a watcher for the specified folder
            seer = new FileSystemWatcher(_searchPath);
            Console.WriteLine(_searchPath);
            // watch for spefici changes if the filename change, directory chages, attribures, creation
            seer.NotifyFilter = NotifyFilters.DirectoryName | NotifyFilters.FileName |
                NotifyFilters.Attributes | NotifyFilters.CreationTime;
            // but only monitor *.log files
            //seer.Filter = "*log*";
            // setup events so we can respond

            seer.Changed += new FileSystemEventHandler(seer_Changed);
            seer.Created += new FileSystemEventHandler(seer_Changed);
            seer.Deleted += new FileSystemEventHandler(seer_Changed);
            seer.Renamed += new RenamedEventHandler(seer_Changed);

            //start it up
            seer.EnableRaisingEvents = true;
            _isrunning = true;
        }

        public void Start()
        {

        }

        public void Stop()
        {
            //stop it
            seer.EnableRaisingEvents = false;
            _isrunning = false;
        }


        void seer_Changed(object sender, FileSystemEventArgs e)
        {
            WatchedDataChange(sender, e);
        }
        public delegate void OnWatchedDataChange(object sender, FileSystemEventArgs e);
        public event OnWatchedDataChange WatchedDataChange;
    }
}

